package reply

import (
	"context"
	"fmt"
	"strconv"
	"strings"

	"mall/common/errorx"
	"mall/common/globalkey"
	"mall/common/mqueuex"
	"mall/service/backend/api/internal/lib/membertask"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/backend/common/utils/pointy"
	"mall/service/forum/rpc/forumclient"

	"github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePostLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePostLogic {
	return &UpdatePostLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdatePostLogic) UpdatePost(req *types.UpdateForumPostReq) (*types.BaseMsgResp, error) {
	if len(req.Ids) == 0 {
		return nil, errorx.Error(errorx.ERROR_FORUM_POST_ID_REQUIRED, "")
	}

	var adminId uint64 = l.GetUserId()

	var userTaskForumQuestionGem string
	memberTaskMap := l.getMemberTaskMap()
	if memberTaskMap != nil {
		logrus.Info(fmt.Sprintf("UpdatePost memberTaskMap: %+v", memberTaskMap))
		if memberTask, ok := memberTaskMap[mqueuex.UserTaskForumQuestion]; ok { // "forum_topic"
			if memberTask.Gems != nil && *memberTask.Gems > 0 {
				userTaskForumQuestionGem = strconv.FormatInt(int64(*memberTask.Gems), 10)
			}
		}
	}

	logrus.Info(fmt.Sprintf("UpdatePost userTaskForumReviewGem: %+v", userTaskForumQuestionGem))

	var ids []string
	for _, id := range req.Ids {
		ids = append(ids, strconv.FormatInt(int64(id), 10))
	}
	var modType int32 = globalkey.ForumModTypeReply // 回答审核
	if req.Sticky != nil {
		modType = globalkey.ForumModTypeReplySticky // 回答推荐
	}
	_, err := l.svcCtx.ForumRpc.BackendBatchUpdatePost(l.ctx, &forumclient.BackendPostListReq{
		Ids:       pointy.GetPointer(strings.Join(ids, ",")),
		Type:      pointy.GetPointer(uint32(modType)),
		Status:    req.Status,
		Sticky:    req.Sticky,
		Memo:      req.Memo,
		Extra:     req.Extra,
		Gems:      pointy.GetPointer(userTaskForumQuestionGem),
		UpdatedBy: pointy.GetPointer(adminId),
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{
		Success: true,
		Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
	}, nil
}

func (l *UpdatePostLogic) GetUserId() uint64 {
	var userId uint64
	if val := l.ctx.Value("userId"); val != nil {
		if i, err := strconv.ParseInt(val.(string), 10, 64); err == nil {
			userId = uint64(i)
		}
	}

	return userId
}
func (l *UpdatePostLogic) getMemberTaskMap() (map[string]*types.MemberTaskInfo) {
	memberTask := membertask.NewMemberTask(l.ctx, l.svcCtx)
	memberTaskList, err := memberTask.List()
	if err != nil {
		logrus.Info(fmt.Sprintf("UpdatePost getMemberTaskMap err: %+v", err))
		return nil
	}

	if len(memberTaskList) > 0 {
		resp := make(map[string]*types.MemberTaskInfo)
		for _, v := range memberTaskList {
			if v.Slug != nil && len(*v.Slug) > 0 {
				resp[*v.Slug] = v
			}
		}

		return resp
	}

	return nil
}
